package gui.listener;

import entity.Category;
import gui.panel.CategoryPanel;
import gui.panel.MainPanel;
import gui.panel.RecordPanel;
import gui.panel.SpendPanel;
import service.RecordService;
import util.GUIUtil;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;

public class RecordListener implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
        RecordPanel recordPanel = RecordPanel.instance;

        if(0== recordPanel.cbModel.cs.size()) {
        //if (recordPanel.cbModel.cs.isEmpty()) {
            JOptionPane.showMessageDialog(recordPanel,"暂无消费分类，无法添加，请先增加消费分类");
            MainPanel.instance.workingPanel.show(CategoryPanel.instance);
            return;
        }

        if(!GUIUtil.checkZero(recordPanel.tfSpend,"花费金额"))
            return;

        int spend = Integer.parseInt(recordPanel.tfSpend.getText());
        Category c = (Category) recordPanel.cbCategory.getSelectedItem();
        //Category c1 = (Category) recordPanel.cbModel.getSelectedItem();

        String comment = recordPanel.tfComment.getText();
        Date d = recordPanel.datePicker.getDate();

        new RecordService().add(spend,c,comment,d);

        JOptionPane.showMessageDialog(recordPanel, "添加成功");

        MainPanel.instance.workingPanel.show(SpendPanel.instance);
    }
}
